package com.kuang.config;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * @program: springboot-06-security
 * @description:
 * @author: 陈恩涛
 * @create: 2020-10-27 16:32
 **/

//AOP
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    //链式编程
    //授权
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //首页所有人都可以访问，功能页只有对应有权限的人才能访问

        //请求授权的规则
        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");

        //没有权限默认会到登录页面,需要开启登录的页面
        //定制登录页
        http.formLogin()
                .usernameParameter("username")
                .passwordParameter("password")
                .loginPage("/toLogin")//登录页面
                .loginProcessingUrl("/login")//登录表单提交请求
                .defaultSuccessUrl("/index");//默认成功登录跳转到"/"
        //注销：开启了注销功能,注销后跳到首页

        //防止网站攻击工具：     get,post
        http.csrf().disable();//关闭csrf功能:登出失败可能的原因~

        http.logout().logoutSuccessUrl("/");

        //开启记住我功能:cookie默认保存两周
        http.rememberMe().rememberMeParameter("remember");

    }

    //认证    springboot 2.1.x 可以直接使用~
    //密码编码：PasswordEncoder
    //在Spring security 5.0+ 新增了很多加密的方法~
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        //这些数据正常应该从数据库中读
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("kuangshen").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3").and()
                .withUser("root").password(new BCryptPasswordEncoder().encode("88888888")).roles("vip1","vip2","vip3").and()
                .withUser("guest").password(new BCryptPasswordEncoder().encode("654321")).roles("vip1");
    }
}
